﻿package 
{
	import flash.events.Event;
	import flash.display.MovieClip;
	
	public class Person extends MovieClip 
	{
		private static const Speed:int = 5;
		private function get personNumber():int
		{
			var tempArray:Array = this.name.split(Constants.MessageSplitCharacter);
			return tempArray[2];
		}
		
		public function Person() 
		{
			init();
			addEventListener(Event.ENTER_FRAME, EnterFrame);
			addEventListener(Event.REMOVED, Unload);
		}
		
		private function init():void
		{
			if (body != null)
			{
				body.stop();
			}
			holdFrame();
		}
		
		private function Unload(event:Event):void
		{
			if (event.target.name == this.name)
			{
				removeEventListener(Event.ENTER_FRAME, EnterFrame);
			}
		}
		
		private function EnterFrame(event:Event):void
		{
			//this.parent is background
			var tempBackground:Background = (this.parent as Background);
			
			if (tempBackground != null)
			{
				var overworld:Overworld = (tempBackground.parent as Overworld);
				if (overworld != null)
				{
					if (talkDown.hitTestObject(overworld.Player))
					{
						moveFrame(1);
					}
					else if (talkUp.hitTestObject(overworld.Player))
					{
						moveFrame(2);
					}
					else if (talkRight.hitTestObject(overworld.Player))
					{
						moveFrame(3);
					}
					else if (talkLeft.hitTestObject(overworld.Player))
					{
						moveFrame(4);
					}
					holdFrame();
					Stop();
				}
			}
		}
		
		private function holdFrame():void
		{
			if (body != null)
			{
				if (body.larm != null)
				{
					body.larm.gotoAndStop(personNumber);
				}
				if (body.rarm != null)
				{
					body.rarm.gotoAndStop(personNumber);
				}
				if (body.rleg != null)
				{
					body.rleg.gotoAndStop(personNumber);
				}
				if (body.lleg != null)
				{
					body.lleg.gotoAndStop(personNumber);
				}
				if (body.body != null)
				{
					body.body.gotoAndStop(personNumber);
				}
				if (body.head != null)
				{
					body.head.gotoAndStop(personNumber);
				}
			}
		}
		
		private function moveFrame(moveDirection:int):void
		{
			switch(moveDirection)
			{
				case (1)://Down
					gotoAndStop(1);
				break;
				case (2)://Up
					gotoAndStop(2);
				break;
				case (3)://Right
					gotoAndStop(3);
				break;
				case (4)://Left
					gotoAndStop(4);
				break;
			}
		}
		
		public function Move(moveDirection:int, endStop:Boolean):void
		{
			if (body != null)
			{
				body.play();
			}
			switch(moveDirection)
			{
				case (1)://Down
					y += Speed;
				break;
				case (2)://Up
					y -= Speed;
				break;
				case (3)://Right
					x += Speed;
				break;
				case (4)://Left
					x -= Speed;
				break;
			}
			
			moveFrame(moveDirection);
			holdFrame();
			
			if (endStop)
			{
				Stop();
			}
		}

		public function Stop():void
		{
			if (body != null)
			{
				body.stop();
			}
		}
		
		public function MovementCommand(moveNumber:int):void
		{
			var down:int = 1;
			var up:int = 2;
			var right:int = 3;
			var left:int = 4;
			switch(moveNumber)
			{
				case 0:
					Move(right, true);
				break;
			}
		}
	}
}